/*
 * File:   SynchronizedPriorityQueue.h
 * Author: henrya
 *
 * Created on June 27, 2009, 5:02 PM
 */

#ifndef _SYNCHRONIZEDPRIORITYQUEUE_H
#define	_SYNCHRONIZEDPRIORITYQUEUE_H

#include <queue>
#include <pthread.h>
#include <vector>
#include <limits>
using std::numeric_limits;

#include "Message.h"

using std::vector;
using std::priority_queue;

class MessageComparator {
public:

    bool operator() (const Message* lhs, const Message* rhs) const {
    if (lhs->getType() != rhs->getType()) {
    return lhs->getType() > rhs->getType();
    } else {
        return lhs->getSN() > rhs->getSN();
    }
    }
};

class SynchronizedPriorityQueue {
public:
    SynchronizedPriorityQueue(unsigned int size = numeric_limits<unsigned int>::max(), bool isHop = true);
    virtual ~SynchronizedPriorityQueue();
    bool push(Message* msg);
    Message* pop();
    unsigned int size();
    unsigned int getMessagePassCount();
    void resetMessagePassCount();
private:
    priority_queue<Message*, vector<Message*>, MessageComparator> queueue;
    pthread_mutex_t classLock;
    unsigned int maxSize;
    bool isHop;
    unsigned int messagePass;
};

#endif	/* _SYNCHRONIZEDPRIORITYQUEUE_H */

